<template>
  <!--登录-->
  <div class="login">
    <p class="banner"><img src="../../assets/banner.png" alt="" width="100%"></p>
    <wv-group>
      <wv-input placeholder="请输入用户名" type="text" v-model="accountName" ></wv-input>
      <wv-input placeholder="请输入密码" :type="type" v-model="password" class="pwd">
        <img :src="src" alt="" slot="ft" @click="change" width="30">
      </wv-input>
    </wv-group>
    <wv-button type="primary" @click="login">登录</wv-button>
  </div>
</template>

<script type="text/ecmascript-6">
  import {getStore, setStore} from '../../../test/unit/storage'
  import {getOpenId, login, getuserInfo} from '../../../test/unit/http'
  import {Toast} from 'we-vue'
  export default {
    name: 'login',
    props: {},
    data() {
      return {
        type: 'password',
        src: require('../../assets/bieye.png'),
        accountName: "",
        password: ""
      };
    },
    methods: {
      change(){
        this.type = this.type == 'password' ? 'text' : 'password';
        this.src = this.src == require('../../assets/eye.png') ? require('../../assets/bieye.png') : require('../../assets/eye.png')
      },
      gologin(params){
        login(params).then(res => {
          console.log(res);
          if (res.code === 100) {
            var isLogin = res.object.isLogin;
            var employeeType = res.object.employeeType;
            var token = res.object.token;
            var employeePosition = res.object.employeePosition;
            setStore('token', token);
            setStore('employeeType', employeeType);
            setStore('employeePosition', employeePosition);
            if (isLogin == "" || isLogin == "1") {
              this.$router.push("/firstlogin")
            } else {
              if (employeeType == "platform") {
                this.$router.push("/index")
              }
              if (employeeType == "shop") {
                this.$router.push("/shangjiaindex")
              }
              if (employeeType == "third") {
                this.$router.push("/shangjiaindex")
              }
            }
          }else{
            Toast.text(res.info)
          }
        })
      },
      login(){
        var checkResult = this.checkFrom;
        if (!checkResult.result) {
          Toast.text(checkResult.info);
          return;
        }
        let params = {
          accountName: this.accountName,
          password: this.password
        };
        this.gologin(params);
      }
    },
    created() {
      getuserInfo({}).then(res => {
        if (res.code === 100) {
          var isLogin = res.object.isLogin;
          var employeeType = res.object.employee.employeeType;
          if (isLogin == "" || isLogin == "1") {
            this.$router.push("/firstlogin")
          } else {
            if (employeeType == "platform") {
              this.$router.push("/index")
            }
            if (employeeType == "shop") {
              this.$router.push("/shangjiaindex")
            }
            if (employeeType == "third") {
              this.$router.push("/shangjiaindex")
            }
          }
        }
      })
    },
    computed: {
      checkFrom: function () {
        var msg = {};
        if (this.$myUtils.isNull(this.accountName)) {
          msg['result'] = false;
          msg['info'] = '请输入用户名';
          return msg;
        }
        if (this.$myUtils.isNull(this.password)) {
          msg['result'] = false;
          msg['info'] = '请输入密码';
          return msg;
        }
        msg['result'] = true;
        msg['info'] = '校验成功';
        return msg;
      }
    },
    components: {}
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
  .banner img {
    object-fit: cover;
  }

  .weui-btn_primary {
    background-color: #f98e3c;
    margin-top: 30px;
  }

  button.weui-btn {
    width: 94%;
    margin-top: 30px;
  }

  .weui-btn_primary a {
    color: #fff;
  }

  .weui-cell:before {
    right: 15px;
  }
</style>
